<?php

namespace App\Http\Resources\Train\Transfer;

use App\Trip\Train\TrainType;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

class TrainsToTypeResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     *
     * @return array|Arrayable|JsonSerializable
     */
    public function toArray($request)
    {
        $trains = parent::toArray($request);
        $names  = collect($trains)->map(function ($train_code) {
            $type_name = TrainType::getTrainTypeName($train_code);
            return [
                'name'    => $type_name,
                'name_uy' => TrainType::getTrainTypeNameTranslation($type_name, 'uy'),
            ];
        });
        return [
            'name'    => $names->implode("name", '转'),
            'name_uy' => $names->implode("name_uy", " <".TrainType::getTrainTypeNameTranslation('转', 'uy')."> "),
        ];
    }
}
